/*
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2014
 *
 * fileName:  SessionProxyFilter.java
 * author:  lei
 * Company:  Cloudland Studio
 * Createdate:  2013-9-14 下午9:26:08
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2013-9-14          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.web.filter;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.cloudland.web.context.request.SessionProxyRequest;

 /**
 * <pre> 
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  SessionProxyFilter
 * @author  lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
public class SessionProxyFilter implements Filter {
	
	/**
     * LOGGER 日志对象
     */
    private static final Logger LOGGER = Logger.getLogger(SessionProxyFilter.class);
	
    /**
     * cookieName 用于获取本地存储的 标示符名称
     */
    private String cookieName = "ltn"; 
    
	/**
	 * @param filterConfig
	 * @throws ServletException
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("SessionProxyFilter initialization method is not implemented.");
		}
	}
	
	/**
	 * @param request
	 * @param response
	 * @param chain
	 * @throws IOException
	 * @throws ServletException
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("SessionProxyFilter filter begins processing...");
        }
		
		String jsessionId = null;
        Cookie[] cookies = ((HttpServletRequest)request).getCookies();
        if (null != cookies) {
            for (Cookie co : cookies) {
                if (cookieName.equals(co.getName())) {
                    jsessionId = co.getValue();
                    break;
                }
            }
        }
        
        if (null == jsessionId || jsessionId.length() <= 0) {
            jsessionId = UUID.randomUUID().toString().replaceAll("-", "");
            Cookie cookie = new Cookie(cookieName, jsessionId);
            cookie.setMaxAge(60 * 20);
            ((HttpServletResponse)response).addCookie(cookie);
        }
		
        // 创建代理 Session
        chain.doFilter(new SessionProxyRequest((HttpServletRequest)request, jsessionId), response);
	}
	
	/**
	 * 
	 */
	public void destroy() {
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("SessionProxyFilter destruction method is not implemented.");
		}
	}

}
